import { Controller, Logger } from '@nestjs/common';
import {
  Ctx,
  EventPattern,
  MessagePattern,
  Payload,
  RmqContext,
} from '@nestjs/microservices';
import { Observable, of } from 'rxjs';

@Controller()
export class BrokerController {
  @EventPattern('emit')
  async handleUserCreated(
    @Payload() data: string,
    @Ctx() context: RmqContext,
  ): Promise<Observable<string>> {

    Logger.debug('EventPattern' + data + context.getPattern(), 'handleUserCreated');
    return of(data);
  }

  @MessagePattern({ cmd: 'sum' })
  async sum(
    @Payload() data: number,
    @Ctx() context: RmqContext,
  ): Promise<number> {

    Logger.debug(data, 'MessagePattern');
    return data * 200;
  }
}
